using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Services;
using Scopa;
using Scopa.Menu;
using Scopa.GameSound;

namespace Scopa.GamePlay
{
  using System.Text;
    using Locale = ScopaLocale;

  public class RecapRenderer : UIRenderer
  {
    Texture2D background, yes, no;
    Texture2D carte, denari, primiera, settebello, scope, totale;
    SpriteFont font;
    Button OkButton;
    private bool intro = true;

    //public float elapsedTime { get; set; }
    public RecapRenderer(Game game)
      : base(game)
    {
      //UpdateOrder = (int)ComponentOrders.UI;     
    }

    protected override void Dispose(bool disposing)
    {
      //Game.Services.RemoveService(typeof(IUIRenderer));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      base.Initialize();

      OkButton = new Button(Game, @"Images//Menu//button2", new Rectangle(350, 400, 100, 40), Color.Green, "small_font", "Ok", Color.Yellow);
     
      Outro = false;
    }

    protected override void LoadContent()
    {
      large_font = Game.Content.Load<SpriteFont>(@"large_font");
      font = Game.Content.Load<SpriteFont>(@"baveuse1_font");

      background = Game.Content.Load<Texture2D>(@"Images\\Menu\\SquarePaper");
      yes = Game.Content.Load<Texture2D>(@"Images\\Menu\\okScopa");
      no = Game.Content.Load<Texture2D>(@"Images\\Menu\\no");

      carte = Game.Content.Load<Texture2D>(@"Images\\Menu\\LungoCarte");
      denari = Game.Content.Load<Texture2D>(@"Images\\Menu\\Denari");
      settebello = Game.Content.Load<Texture2D>(@"Images\\Menu\\SettebelloAlone");
      primiera = Game.Content.Load<Texture2D>(@"Images\\Menu\\Settanta3D");
      scope = Game.Content.Load<Texture2D>(@"Images\\Menu\\Scope");

      base.LoadContent();
    }

   
    public override void Update(GameTime gameTime)
    {
      base.Update(gameTime);
    }

    private int timer = 0;
    public override void UpdateRecap(GameTime gametime)
    {
      var boardRenderer = Game.Services.GetService(typeof(IBoardRenderer)) as BoardRenderer;

      if (((Game1)Game).IsBackButtonPressed())
      {
        OkButton.color = Color.Red;
        //if (OnMatchExit != null)
        //  OkButton();
      }

      foreach (var g in cachedGestures)
      {
        switch (g.GestureType)
        {
          case GestureType.Tap:
            // Check if menu button was tapped
            if (OkButton.IsInBounds(g.Position.ToPoint(screen_transform)))
            {
              OkButton.color = Color.Red;

              if (Game.GetScopaContext().QuickGame || Game.GetScopaContext().GameFinished)
              {
                SaveData.SaveUtils.DeleteGame(Game);
                Game.GetScopaContext().currentConfig.hasSavedGame = false;
                ScopaStateManagement.ScopaStateManager.StartMenu(Game);
              }
              else
              {
                gameState.CurrentPlayerIndex++;
                Game.GetScopaContext().PlayerTurn = gameState.CurrentPlayerIndex % gameState.Players.Count;
                ScopaStateManagement.ScopaStateManager.ContinueLevel(Game);
              }
            }
            break;
        }
      }

      if (intro)
      {
        int speed = 20;
        rect.Width += speed;
        rect.Height += speed;

        if(rect.Width == 700) intro = false;
      }

      if (Game.GetScopaContext().GameFinished)
      {
        timer++;

        if (timer > 20)
        {
          if (colorWinner == Color.Blue) colorWinner = Color.Red;
          else colorWinner = Color.Blue;
        }
      }
    }
  

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);
    }

    Rectangle rect = new Rectangle(400, 240, 500, 180);

    public override void DrawScore(GameTime gameTime)
    {
      // Draw a grey curtain
      spriteBatch.Draw(whitePx, gameBounds, null, Color.Lerp(Color.Gray, Color.Transparent, 0.3f), 0.0f, Vector2.Zero, SpriteEffects.None, 0.7f);

      spriteBatch.Draw(background, rect, null, Color.White, 0.0f, new Vector2(400, 240), SpriteEffects.None, 0.85f);

      if (!intro)
      {
        // Titolo
        spriteBatch.DrawString(font, Locale.Strings.Score, new Vector2(60, 50), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

        // Icone
        spriteBatch.Draw(carte, new Rectangle(345, 80, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
        spriteBatch.Draw(denari, new Rectangle(400, 80, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
        spriteBatch.Draw(primiera, new Rectangle(455, 80, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
        spriteBatch.Draw(settebello, new Rectangle(505, 80, 45, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
        spriteBatch.Draw(scope, new Rectangle(555, 80, 45, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);

        for (int i = 0; i < gameState.Players.Count; i++)
        {
          DrawPoints(i, 130 + (70 * i));
          DrawCards(i, gameState.Players.Count);
        }

        // Draw menu button
        spriteBatch.Draw(OkButton.texture, OkButton.bounds, null, OkButton.color, 0, Vector2.Zero, SpriteEffects.None, 0.99f);
        spriteBatch.DrawString(OkButton.spriteFont, OkButton.message, new Vector2((float)OkButton.bounds.Center.X, (float)OkButton.bounds.Center.Y), OkButton.textColor, 0, OkButton.spriteFont.MeasureString(OkButton.message) / 2, OkButton.scale, SpriteEffects.None, 1.0f);

      }
    }


    private Color colorWinner = Color.Blue;
    private void DrawPoints(int p, int y)
    {

      String mess = gameState.Players[p].Name;

      if (gameState.Players[p].Win)
        spriteBatch.DrawString(large_font, mess, new Vector2(70, y), colorWinner, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
      else
        spriteBatch.DrawString(large_font, mess, new Vector2(70, y), new Color(50,50,50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

      int x = 200;

      //x = (int)font.MeasureString(gameState.Players[p].Name).X + 100;

      if (gameState.Players[p].Carte) spriteBatch.Draw(yes, new Rectangle(x + 150, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
      else spriteBatch.Draw(no, new Rectangle(x + 150, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);

      if (gameState.Players[p].Denari) spriteBatch.Draw(yes, new Rectangle(x + 200, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
      else spriteBatch.Draw(no, new Rectangle(x + 200, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);

      if (gameState.Players[p].Primiera) spriteBatch.Draw(yes, new Rectangle(x + 250, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
      else spriteBatch.Draw(no, new Rectangle(x + 250, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);

      if (gameState.Players[p].Settebello) spriteBatch.Draw(yes, new Rectangle(x + 300, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
      else spriteBatch.Draw(no, new Rectangle(x + 300, y, 50, 50), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);

      spriteBatch.DrawString(large_font, gameState.Players[p].ScopaCounter.ToString(), new Vector2(x + 370, y), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

      spriteBatch.DrawString(large_font, gameState.Players[p].Scores.ToString(), new Vector2(x + 450, y), new Color(50, 50, 50), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

    }

    private void DrawCards(int p, int playersCount)
    {
      int countCards = 0;
      int offset = 0;

      for (int t = 0; t < gameState.Players[p].Takes.Count; t++)
      {
        for (int i = 0; i < gameState.Players[p].Takes[t].Cards.Count; i++)
        {
          var c = gameState.Players[p].Takes[t].Cards[i];
          c.Width = 30;
          c.Height = 40;
          c.Rotation = playerAreas[p].cardsRotation;

          countCards++;

          string a = "" + countCards;

          offset += 30;
          Rectangle Card_Rectangle = new Rectangle(offset, 460, c.Width, c.Height);

          if (p == 0) Card_Rectangle = new Rectangle(offset, 460, c.Width, c.Height);

          if (p == 1)
          {
            if (playersCount < 3) Card_Rectangle = new Rectangle(offset, 20, c.Width, c.Height);
            else Card_Rectangle = new Rectangle(770, 50 + offset, c.Width, c.Height); 
          }

          if (p == 2) Card_Rectangle = new Rectangle(50 + offset, 20, c.Width, c.Height);

          if (p == 3) Card_Rectangle = new Rectangle(30, offset, c.Width, c.Height);

          spriteBatch.Draw(Deck_Texture, Card_Rectangle, c.SourceRectangle, Color.White, c.Rotation, new Vector2(c.SourceRectangle.Width / 2, c.SourceRectangle.Height / 2), SpriteEffects.None, c.Depth);

          //spriteBatch.DrawString(font, a, new Vector2(200, 200 + (p * 140)), Color.White, 0.0f, Vector2.Zero, 0, SpriteEffects.None, 1.0f);
        }
      }

      spriteBatch.DrawString(large_font, countCards.ToString(), new Vector2(30, 20 + (p * 440)), Color.Black);
    }

    public override event OnExit OnExit;
  }
}
